package com.cxyd.modules.wms.controller;

import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.security.user.UserDetail;
import com.cxyd.modules.wms.dto.ErpReelDTO;
import com.cxyd.modules.wms.dto.TaskDTO;
import com.cxyd.modules.wms.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 业务记录
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/task")
public class TaskController {
    private final TaskService taskService;

    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @GetMapping("page")
    //@RequiresPermissions("wms:task:page")
    public Result<PageData<TaskDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<TaskDTO> page = taskService.page(params);

        return new Result<PageData<TaskDTO>>().ok(page);
    }
    @GetMapping("contbacklist")
    //@RequiresPermissions("wms:task:page")
    public Result<PageData<TaskDTO>> contBackList(@RequestParam Map<String, Object> params) {
        PageData<TaskDTO> page = taskService.contBackList(params);

        return new Result<PageData<TaskDTO>>().ok(page);
    }
    @GetMapping("count")
    //@RequiresPermissions("wms:task:page")
    public int taskEmptyCount(@RequestParam Map<String, Object> params) {

        return taskService.selectEmptyCount(params);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:task:info")
    public Result<TaskDTO> get(@PathVariable("id") Long id) {
        TaskDTO data = taskService.get(id);

        return new Result<TaskDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:task:save")
    public Result save(@RequestBody TaskDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        taskService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:task:update")
    public Result update(@RequestBody TaskDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        taskService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:task:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        taskService.delete(ids);

        return new Result();
    }

    @PostMapping("retask/{id}")
    @LogOperation("取消任务")
    public Map<String, Object> retask(@PathVariable("id") Long id) {
        UserDetail user = SecurityUser.getUser();
        //效验数据
        final Map<String, Object> map = new HashMap<>();
        map.put("taskId", id);
        map.put("userId", user.getId());
        taskService.taskCancel(map);
        return map;
    }

    @PostMapping("request-out")
    @LogOperation("出库请求")
    //@RequiresPermissions("wms:task:out")
    public Result requestOut(@RequestBody Map<String, Object> params) {
        ProcResult procResult = taskService.requestContOut(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("request-in")
    @LogOperation("入库请求")
   // @RequiresPermissions("wms:task:in")
    public Result requestIn(@RequestBody Map<String, Object> params) {
        ProcResult procResult = taskService.requestContIn(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("contback")
    @LogOperation("放行请求")
    // @RequiresPermissions("wms:task:in")
    public Result contBack(@RequestBody Map<String, Object> params) {
        ProcResult procResult = taskService.contBack(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("affirm")
    @LogOperation("揀貨確認")
    // @RequiresPermissions("wms:task:in")
    public Result taskAffi(@RequestBody Map<String, Object> params) {
        ProcResult procResult = taskService.taskAffirm(params);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }



    @GetMapping("reelid/{reelId}")
    //@RequiresPermissions("wms:task:reelid")
    @LogOperation("reelid请求")
    public Result<PageData<ErpReelDTO>> reelAsk(@PathVariable("reelId") String reelId) {
        PageData<ErpReelDTO> page = taskService.reelAsk(reelId);

        if(page.getList().stream().count()==0){
            return new Result<PageData<ErpReelDTO>>().error("此reelid["+reelId+"]无入库数据");
        }

        if(page.getList().get(0).getSupplier() == null
        || page.getList().get(0).getSupplier().isEmpty()){
            return new Result<PageData<ErpReelDTO>>().error("此reelid["+reelId+"]無庫別資料，請建立正確庫別資料");
        }
        return new Result<PageData<ErpReelDTO>>().ok(page);
    }

    @GetMapping("saferate")
    public String getSafeRate() {

        return taskService.getSafeRate();
    }
}